package com.lixin.admin.server.controller;

import com.lixin.admin.server.service.RoleService;
import com.lixin.admin.server.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "角色相关接口")
public class RoleController {
    @Resource
    private RoleService roleService;

    @GetMapping("/role")
    @ApiOperation("获取角色列表")
    public CommonResponseVO<List<RoleVO>> getList(
            @ApiParam("当前页") @RequestParam("p") Long page,
            @ApiParam("每页记录数") @RequestParam("ps") Long pageSize
    ) {
        List<RoleVO> roleVOList = roleService.getList(page, pageSize);
        return new CommonResponseVO<>(ResponseCode.SUCCESS, roleVOList);
    }

    @GetMapping("/role/{id}")
    @ApiOperation("获取角色信息")
    public CommonResponseVO<RoleVO> get(@ApiParam("角色ID") @PathVariable("id") Long id) {
        RoleVO roleVO = roleService.get(id);
        return new CommonResponseVO<>(ResponseCode.SUCCESS, roleVO);
    }

    @PostMapping("/role/create")
    @ApiOperation("新增角色")
    public CommonResponseVO<Boolean> create(@ApiParam("新增角色信息") @RequestBody RoleCreateVO roleCreateVO) {
        boolean res = roleService.create(roleCreateVO);
        if (res) {
            return new CommonResponseVO<>(ResponseCode.SUCCESS, res);
        } else {
            return new CommonResponseVO<>(ResponseCode.FAIL, res);
        }
    }

    @PutMapping("/role/modify")
    @ApiOperation("修改角色")
    public CommonResponseVO<Boolean> modify(@ApiParam("修改角色信息") @RequestBody RoleUpdateVO roleUpdateVO) {
        boolean res = roleService.modify(roleUpdateVO);
        if (res) {
            return new CommonResponseVO<>(ResponseCode.SUCCESS, res);
        } else {
            return new CommonResponseVO<>(ResponseCode.FAIL, res);
        }
    }

    @DeleteMapping("/role/del")
    @ApiOperation("删除多个角色")
    public CommonResponseVO<Boolean> del(@ApiParam("删除角色ID") @RequestBody RoleDelVO roleDelVO) {
        boolean res = roleService.del(roleDelVO);
        if (res) {
            return new CommonResponseVO<>(ResponseCode.SUCCESS, res);
        } else {
            return new CommonResponseVO<>(ResponseCode.FAIL, res);
        }
    }
}
